package com.apexedu.portal.service.impl;

import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.entity.TSysMenu;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录处理类。包括系统管理员也集成在此登录。
 * 
 * @author Yangjianliang datetime 2011-2-26
 */
@SuppressWarnings("serial")
public class AjaxProblem extends HttpServlet {

	private static Log LOG = LogFactory.getLog(AjaxProblem.class);

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		executeAction(request, response, false);
	}

	/**
	 * 和post的区别就是参数需要解码，以支持中文昵称登录
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		executeAction(request, response, true);
	}
	
	private static void executeAction(HttpServletRequest request, HttpServletResponse response, boolean needDecode) throws IOException{
		request.setCharacterEncoding("UTF-8");//处理post中文问题
		SimpleDateFormat sdf_time = new SimpleDateFormat("yyyy-MM-dd");
		String message="ok";
		Map<String, Object> outData = new HashMap<String, Object>();
		HttpSession session = request.getSession(true);
		String menuid = request.getParameter("menuid");
		String userid = request.getParameter("userid");
		String tid=request.getParameter("tid");
		String pproblem=request.getParameter("pproblem");
		String pcontent=request.getParameter("pcontent");
		TSysMenu tsysmenu =DBUtil.queryBean("select * from t_sys_menu t where t.menuid=?",TSysMenu.class,menuid);
        String insert_sql=" insert into T_PROBLEM (WID,PPROBLEM,PTIME,PUSER,PCONTENT,TID,SID,ZYMC) " +
						  " values ('"+SeqFactory.getNewSequenceAlone()+"','"+pproblem+"','"+sdf_time.format(new Date())+"','"+userid+"','"+pcontent+"','"+tid+"','"+menuid+"','"+tsysmenu.getMenuname()+"')";
		int i= DBUtil.executeSQL(insert_sql);
		if (i<1){
			message="no";
		}
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(message);
		out.flush();
		out.close();
	}
}
